package com.qiezi.project.contract;

import com.alibaba.fastjson.TypeReference;
import com.qiezi.bean.AgentBean;
import com.qiezi.bean.DataBean;
import com.qiezi.bean.PaypalBean;
import com.qiezi.bean.ResponseBean;
import com.qiezi.bean.TokenBean;
import com.qiezi.mvp.BaseMvpView;
import com.qiezi.mvp.BasePresenter;
import com.qiezi.util.AESUtil;
import com.qiezi.util.Request;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;



public class InvitationContract {
    public static class Presenter extends BasePresenter<View>{

        private RequestCall cell;

        public void canceledCell(){
            if (cell != null)cell.cancel();
            mView.complete();
        }

        public void modAddr(String addr){
            if (cell != null) cell.cancel();
            if (params == null){
                params = new HashMap<>();
            }
            params.clear();
            params.put("addr",addr);

            cell = Request.post(Request.Api.modAddr, params, new Request.JsonCallback<Object>(Object.class) {
                @Override
                public void onSueccess(ResponseBean data) {
                    if (mView == null)return;
                    mView.onModAddr();
                }

                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

            });
        }



        public void paypal(String money){
            if (cell != null) cell.cancel();
            if (params == null){
                params = new HashMap<>();
            }
            params.clear();
            params.put("amount",money);
            cell = Request.post(Request.Api.paypal, params, new Request.JsonCallback<Object>(Object.class) {
                @Override
                public void onSueccess(ResponseBean data) {
                    if (mView == null)return;
                    mView.onPaypal();
                }

                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }
            });
        }

        public void getPaypal(){
            if (cell != null) cell.cancel();
            cell = Request.post(Request.Api.getPaypal, null, new Request.JsonCallback<DataBean<PaypalBean>>(new TypeReference<DataBean<PaypalBean>>(){}.getType()) {

                @Override
                public void onSueccess(ResponseBean<DataBean<PaypalBean>> data) {
                    if (mView == null)return;
                    mView.onPaypalList((ArrayList<PaypalBean>) data.getData().getList());
                }

                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.onPaypalError();
                }


            });
        }



        public void loadData(){
            if (cell != null) cell.cancel();
            cell = Request.post(Request.Api.getAgentInfo, null, new Request.JsonCallback<AgentBean>(AgentBean.class) {
                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

                @Override
                public void onSueccess(ResponseBean<AgentBean> data) {
                    if (mView == null)return;
                    mView.onResultData(data.getData());
                }

            });
        }

        private Map<String,String> params;

        public void createUser(String user,String pwd,String address){
            if (params == null){
                params = new HashMap<>();
            }
            params.clear();
            params.put("username",user);
            params.put("password",AESUtil.md5(pwd));
            params.put("addr",address);
            if (cell != null) cell.cancel();
            cell = Request.post(Request.Api.openAgent, params, new Request.JsonCallback(Object.class) {

                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

                @Override
                public void onSueccess(ResponseBean data) {
                    if (mView == null)return;
                    mView.onCreateUser();
                }

            });
        }


        public void logout(){
            if (cell != null) cell.cancel();
            cell = Request.post(Request.Api.agentLogout, null, new Request.JsonCallback(Object.class) {


                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

                @Override
                public void onSueccess(ResponseBean data) {
                    if (mView == null)return;
                    mView.onLogout();
                }

            });
        }


        public void login(String user,String pwd){
            if (params == null){
                params = new HashMap<>();
            }
            params.clear();
            params.put("username",user);
            params.put("password",AESUtil.md5(pwd));
            if (cell != null) cell.cancel();
            cell = Request.post(Request.Api.agentLogin, params, new Request.JsonCallback<TokenBean>(TokenBean.class) {


                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

                @Override
                public void onSueccess(ResponseBean<TokenBean> data) {
                    if (mView == null)return;
                    mView.onLogin(data.getData().getToken());
                }

            });
        }
    }

    public interface View extends BaseMvpView {
        void onResultData(AgentBean data);
        void onCreateUser();
        void onLogout();
        void onLogin(String token);
        void onPaypalList(ArrayList<PaypalBean> data);
        void onPaypalError();
        void onModAddr();
        void onPaypal();
    }
}